<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center>
		<el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="挂号编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="挂号编码"></el-input>
      </el-form-item>
      <el-form-item label="挂号名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="挂号名称"></el-input>
      </el-form-item>
      <el-form-item label="费用" prop="price">
        <el-input v-model="dataForm.price" placeholder="费用"></el-input>
      </el-form-item>
      <el-form-item label="收费类型" prop="feeType">
        <el-select v-model="dataForm.feeType" placeholder="请选择">
          <el-option v-for="item in feeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号类别" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { getRegisterObj, addRegisterObj, putRegisterObj } from '@/api/eims/register-config.js'

  export default {
    data() {
      return {
				ischeck: false,
        visible: false,
        feeOptions: [
          { value: '', label: '--请选择--' },
          { value: '1', label: '收费' },
          { value: '2', label: '退费' },
        ],
        typeOptions: [
          { value: '', label: '--请选择--' },
          { value: '1', label: '检查费' },
        ],
        dataForm: {
          id: 0,
          code: '',
          name: '',
          price: '',
          feeType: '',
          type: '',
          status: ''
        },
        dataRule: {
          code: [{ required: true, message: '挂号编码不能为空', trigger: 'blur' }],
          name: [{ required: true, message: '挂号名称不能为空', trigger: 'blur' }],
          price: [{ required: true, message: '费用不能为空', trigger: 'blur' }],
          feeType: [{ required: true, message: '收费类型不能为空' }],
          type: [{ required: true, message: '挂号类别不能为空' }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getRegisterObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putRegisterObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
								this.ischeck = false
							})
            } else {
              this.dataForm.status = '1'
              addRegisterObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
								this.ischeck = false
							})
            }
          }else{
						this.ischeck = false
					}
        })
      }
    }
  }
</script>
